package com.jilefojing.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jilefojing.beans.data.response.base.ResponseData;
import com.jilefojing.beans.vo.UploadFile;
import com.jilefojing.controller.base.BaseController;
import com.jilefojing.qiniu.beans.enums.BucketEnum;
import com.jilefojing.qiniu.beans.enums.BucketEnum.BucketEnumExtral_SourceEnum;
import com.jilefojing.util.Detect;
import com.jilefojing.util.QiniuUtil;

@Controller
@RequestMapping("upload")
public class QiniuController extends BaseController {
	@RequestMapping("token/{bucket}")
	public @ResponseBody
	ResponseData getToken(@PathVariable("bucket") String bucketStr, String extral, @RequestParam("suffix") String suffix) {
		BucketEnum bucket = BucketEnum.getBucketByValue(bucketStr);
		BucketEnumExtral_SourceEnum extral_Source = null;
		if (Detect.notEmpty(extral)) {
			extral_Source = BucketEnumExtral_SourceEnum.getBucketExtral_SourceByValue(extral);
		}

		UploadFile uploadFile = new UploadFile();
		uploadFile.setSuffix(suffix);

		ResponseData responseData = new ResponseData();
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("token", QiniuUtil.getToken(bucket));
		resultMap.put("domain", QiniuUtil.getDomainByBucket(bucket));
		resultMap.put("key", QiniuUtil.createKey(bucket, extral_Source, uploadFile));
		responseData.setData(resultMap);
		return responseData;
	}
}
